<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.3.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">
  <meta name="baidu-site-verification" content="code-OyDaGkZ9eq">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"example.com","root":"/","scheme":"Gemini","version":"7.8.0","exturl":true,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":"mac"},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":true,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="本节课程目标 熟悉条件判断语句，如判断整数、判断字符串等 熟悉流程控制语句基本语法，如if…else…">
<meta property="og:type" content="article">
<meta property="og:title" content="03_SHELL编程之条件判断和流程控制">
<meta property="og:url" content="http://example.com/2021/01/03/%E5%B0%8F%E7%94%9F%E5%8D%9A%E5%AE%A2/linux/03_SHELL%E7%BC%96%E7%A8%8B%E4%B9%8B%E6%9D%A1%E4%BB%B6%E5%88%A4%E6%96%AD%E5%92%8C%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6/index.html">
<meta property="og:site_name" content="极风键客">
<meta property="og:description" content="本节课程目标 熟悉条件判断语句，如判断整数、判断字符串等 熟悉流程控制语句基本语法，如if…else…">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="http://example.com/.com//%E6%B5%81%E7%A8%8B%E5%88%A4%E6%96%AD1.png">
<meta property="og:image" content="http://example.com/.com//%E6%B5%81%E7%A8%8B%E5%88%A4%E6%96%AD2.png">
<meta property="og:image" content="http://example.com/.com//%E6%B5%81%E7%A8%8B%E5%88%A4%E6%96%AD3.png">
<meta property="og:image" content="http://example.com/.com//%E6%B5%81%E7%A8%8B%E5%88%A4%E6%96%AD4.png">
<meta property="article:published_time" content="2021-01-03T12:46:25.000Z">
<meta property="article:modified_time" content="2021-02-17T07:26:22.313Z">
<meta property="article:author" content="王增明">
<meta property="article:tag" content="linux">
<meta property="article:tag" content="shell">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://example.com/.com//%E6%B5%81%E7%A8%8B%E5%88%A4%E6%96%AD1.png">

<link rel="canonical" href="http://example.com/2021/01/03/%E5%B0%8F%E7%94%9F%E5%8D%9A%E5%AE%A2/linux/03_SHELL%E7%BC%96%E7%A8%8B%E4%B9%8B%E6%9D%A1%E4%BB%B6%E5%88%A4%E6%96%AD%E5%92%8C%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'en'
  };
</script>

  <title>03_SHELL编程之条件判断和流程控制 | 极风键客</title>
  


  <script data-pjax>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?b94f963e94e4127fa71ab49cbddce93b";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="极风键客" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">极风键客</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">技术狂热爱好者!</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
        <li class="menu-item menu-item-sitemap">

    <a href="/sitemap.xml" rel="section"><i class="fa fa-sitemap fa-fw"></i>站点地图</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>
  <a role="button" class="book-mark-link book-mark-link-fixed"></a>

  <span class="exturl github-corner" data-url="aHR0cHM6Ly9naXRodWIuY29tL3dhbmd6ZW5nbWluZw==" title="客官,来嘛~" aria-label="客官,来嘛~"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></span>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="en">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/01/03/%E5%B0%8F%E7%94%9F%E5%8D%9A%E5%AE%A2/linux/03_SHELL%E7%BC%96%E7%A8%8B%E4%B9%8B%E6%9D%A1%E4%BB%B6%E5%88%A4%E6%96%AD%E5%92%8C%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpeg">
      <meta itemprop="name" content="王增明">
      <meta itemprop="description" content="喜欢折腾的极客聚集地.">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="极风键客">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          03_SHELL编程之条件判断和流程控制
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-01-03 20:46:25" itemprop="dateCreated datePublished" datetime="2021-01-03T20:46:25+08:00">2021-01-03</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2021-02-17 15:26:22" itemprop="dateModified" datetime="2021-02-17T15:26:22+08:00">2021-02-17</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/linux/" itemprop="url" rel="index"><span itemprop="name">linux</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span><br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>7.8k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>7 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h1 id="本节课程目标"><a href="#本节课程目标" class="headerlink" title="本节课程目标"></a>本节课程目标</h1><ul>
<li>熟悉条件判断语句，如判断整数、判断字符串等</li>
<li>熟悉流程控制语句基本语法，如if…else…</li>
</ul>
<a id="more"></a>

<h1 id="一、条件判断语法结构"><a href="#一、条件判断语法结构" class="headerlink" title="一、条件判断语法结构"></a>一、条件判断语法结构</h1><p><strong>思考：何为真(==true==)？何为假(==false==)？</strong></p>
<p>##1. ==条件判断语法格式==</p>
<ul>
<li>格式1： ==<strong>test</strong>== 条件表达式</li>
<li>格式2： <strong>[</strong> 条件表达式 ]   </li>
<li>格式3： <strong>[[</strong> 条件表达式 ]]  支持正则 =~</li>
</ul>
<p><strong>特别说明：</strong></p>
<p>1）==[==  亲亲，我两边都有空格，不空打死你呦  ==]== :imp:</p>
<p>2）==[[==  亲亲，我两边都有空格，不空打死你呦  ==]]==:imp:</p>
<ol start="3">
<li> 更多判断，<code>man test</code>去查看，很多的参数都用来进行条件判断</li>
</ol>
<h2 id="2-条件判断相关参数"><a href="#2-条件判断相关参数" class="headerlink" title="2. 条件判断相关参数"></a>2. 条件判断相关参数</h2><p>问：你要判断什么？</p>
<p>答：我要判断文件类型，判断文件新旧，判断字符串是否相等，判断权限等等…</p>
<h3 id="㈠-判断文件类型"><a href="#㈠-判断文件类型" class="headerlink" title="㈠ ==判断文件类型=="></a>㈠ ==判断文件类型==</h3><table>
<thead>
<tr>
<th>判断参数</th>
<th>含义</th>
</tr>
</thead>
<tbody><tr>
<td>==-e==</td>
<td>判断文件是否存在（任何类型文件）</td>
</tr>
<tr>
<td>-f</td>
<td>判断文件是否存在==并且==是一个普通文件</td>
</tr>
<tr>
<td>-d</td>
<td>判断文件是否存在并且是一个目录</td>
</tr>
<tr>
<td>-L</td>
<td>判断文件是否存在并且是一个软连接文件</td>
</tr>
<tr>
<td>-b</td>
<td>判断文件是否存在并且是一个块设备文件</td>
</tr>
<tr>
<td>-S</td>
<td>判断文件是否存在并且是一个套接字文件</td>
</tr>
<tr>
<td>-c</td>
<td>判断文件是否存在并且是一个字符设备文件</td>
</tr>
<tr>
<td>-p</td>
<td>判断文件是否存在并且是一个命名管道文件</td>
</tr>
<tr>
<td>==-s==</td>
<td>判断文件是否存在并且是一个非空文件（有内容）</td>
</tr>
</tbody></table>
<p><strong>举例说明：</strong></p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">test <span class="literal">-e</span> file					只要文件存在条件为真</span><br><span class="line">[ -<span class="type">d</span> /<span class="type">shell01</span>/<span class="type">dir1</span> ]		 	判断目录是否存在，存在条件为真</span><br><span class="line">[ ! -<span class="type">d</span> /<span class="type">shell01</span>/<span class="type">dir1</span> ]		判断目录是否存在,不存在条件为真</span><br><span class="line">[[ -<span class="type">f</span> /<span class="type">shell01</span>/<span class="number">1</span><span class="type">.sh</span> ]]		判断文件是否存在，并且是一个普通的文件</span><br></pre></td></tr></table></figure>
<h3 id="㈡-判断文件权限"><a href="#㈡-判断文件权限" class="headerlink" title="㈡ 判断文件权限"></a>㈡ 判断文件权限</h3><table>
<thead>
<tr>
<th>判断参数</th>
<th>含义</th>
</tr>
</thead>
<tbody><tr>
<td>-r</td>
<td>当前用户对其是否可读</td>
</tr>
<tr>
<td>-w</td>
<td>当前用户对其是否可写</td>
</tr>
<tr>
<td>-x</td>
<td>当前用户对其是否可执行</td>
</tr>
<tr>
<td>-u</td>
<td>是否有suid，高级权限冒险位</td>
</tr>
<tr>
<td>-g</td>
<td>是否sgid，高级权限强制位</td>
</tr>
<tr>
<td>-k</td>
<td>是否有t位，高级权限粘滞位</td>
</tr>
</tbody></table>
<h3 id="㈢-判断文件新旧"><a href="#㈢-判断文件新旧" class="headerlink" title="㈢ 判断文件新旧"></a>㈢ 判断文件新旧</h3><p>说明：这里的新旧指的是==文件的修改时间==。</p>
<table>
<thead>
<tr>
<th>判断参数</th>
<th>含义</th>
</tr>
</thead>
<tbody><tr>
<td>file1 -nt  file2</td>
<td>比较file1是否比file2新</td>
</tr>
<tr>
<td>file1 -ot  file2</td>
<td>比较file1是否比file2旧</td>
</tr>
<tr>
<td>file1 -ef  file2</td>
<td>比较是否为同一个文件，或者用于判断硬连接，是否指向同一个inode</td>
</tr>
</tbody></table>
<h3 id="㈣-判断整数"><a href="#㈣-判断整数" class="headerlink" title="㈣ 判断整数"></a>㈣ 判断整数</h3><table>
<thead>
<tr>
<th>判断参数</th>
<th>含义</th>
</tr>
</thead>
<tbody><tr>
<td>-eq</td>
<td>相等</td>
</tr>
<tr>
<td>-ne</td>
<td>不等</td>
</tr>
<tr>
<td>-gt</td>
<td>大于</td>
</tr>
<tr>
<td>-lt</td>
<td>小于</td>
</tr>
<tr>
<td>-ge</td>
<td>大于等于</td>
</tr>
<tr>
<td>-le</td>
<td>小于等于</td>
</tr>
</tbody></table>
<h3 id="㈤-判断字符串"><a href="#㈤-判断字符串" class="headerlink" title="㈤ ==判断字符串=="></a>㈤ ==判断字符串==</h3><table>
<thead>
<tr>
<th>判断参数</th>
<th>含义</th>
</tr>
</thead>
<tbody><tr>
<td>-z</td>
<td>判断是否为==空==字符串，字符串长度为0则成立</td>
</tr>
<tr>
<td>-n</td>
<td>判断是否为==非空==字符串，字符串长度不为0则成立</td>
</tr>
<tr>
<td>string1 = string2</td>
<td>判断字符串是否相等</td>
</tr>
<tr>
<td>string1 != string2</td>
<td>判断字符串是否相不等</td>
</tr>
</tbody></table>
<h3 id="㈥-多重条件判断"><a href="#㈥-多重条件判断" class="headerlink" title="㈥ ==多重条件判断=="></a>㈥ ==多重条件判断==</h3><table>
<thead>
<tr>
<th>判断符号</th>
<th>含义</th>
<th>举例</th>
</tr>
</thead>
<tbody><tr>
<td>-a 和 &amp;&amp;</td>
<td>逻辑与</td>
<td>[ 1 -eq 1 -a 1 -ne 0 ]     [ 1 -eq 1 ] &amp;&amp; [ 1 -ne 0 ]</td>
</tr>
<tr>
<td>-o 和 ||</td>
<td>逻辑或</td>
<td>[ 1 -eq 1 -o 1 -ne 1 ]</td>
</tr>
</tbody></table>
<p><strong>==特别说明：==</strong></p>
<p>&amp;&amp;    前面的表达式==为真==，才会执行后面的代码</p>
<p>||     前面的表达式==为假==，才会执行后面的代码</p>
<p>;         ==只==用于==分割==命令或表达式</p>
<h4 id="①-举例说明"><a href="#①-举例说明" class="headerlink" title="① 举例说明"></a>① 举例说明</h4><ul>
<li>数值比较</li>
</ul>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">[<span class="type">root</span>@<span class="type">server</span> ~]<span class="comment"># [ $(id -u) -eq 0 ] &amp;&amp; echo &quot;the user is admin&quot;</span></span><br><span class="line">[<span class="type">root</span>@<span class="type">server</span> ~]<span class="variable">$</span> [ <span class="variable">$</span>(<span class="type">id</span> -<span class="type">u</span>) -<span class="type">ne</span> <span class="number">0</span> ] &amp;&amp; <span class="built_in">echo</span> <span class="string">&quot;the user is not admin&quot;</span></span><br><span class="line">[<span class="type">root</span>@<span class="type">server</span> ~]<span class="variable">$</span> [ <span class="variable">$</span>(<span class="type">id</span> -<span class="type">u</span>) -<span class="type">eq</span> <span class="number">0</span> ] &amp;&amp; <span class="built_in">echo</span> <span class="string">&quot;the user is admin&quot;</span> || <span class="built_in">echo</span> <span class="string">&quot;the user is not admin&quot;</span></span><br><span class="line"></span><br><span class="line">[<span class="type">root</span>@<span class="type">server</span> ~]<span class="comment"># uid=`id -u`</span></span><br><span class="line">[<span class="type">root</span>@<span class="type">server</span> ~]<span class="comment"># test $uid -eq 0 &amp;&amp; echo this is admin</span></span><br><span class="line">this is admin</span><br><span class="line">[<span class="type">root</span>@<span class="type">server</span> ~]<span class="comment"># [ $(id -u) -ne 0 ]  || echo this is admin</span></span><br><span class="line">this is admin</span><br><span class="line">[<span class="type">root</span>@<span class="type">server</span> ~]<span class="comment"># [ $(id -u) -eq 0 ]  &amp;&amp; echo this is admin || echo this is not admin</span></span><br><span class="line">this is admin</span><br><span class="line">[<span class="type">root</span>@<span class="type">server</span> ~]<span class="comment"># su - stu1</span></span><br><span class="line">[<span class="type">stu1</span>@<span class="type">server</span> ~]<span class="variable">$</span> [ <span class="variable">$</span>(<span class="type">id</span> -<span class="type">u</span>) -<span class="type">eq</span> <span class="number">0</span> ]  &amp;&amp; <span class="built_in">echo</span> this is admin || <span class="built_in">echo</span> this is not admin</span><br><span class="line">this is not admin</span><br></pre></td></tr></table></figure>
<ul>
<li>类C风格的数值比较</li>
</ul>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">注意：在(( ))中，=表示赋值；==表示判断</span><br><span class="line">[<span class="type">root</span>@<span class="type">server</span> ~]<span class="comment"># ((1==2));echo $?</span></span><br><span class="line">[<span class="type">root</span>@<span class="type">server</span> ~]<span class="comment"># ((1&lt;2));echo $?</span></span><br><span class="line">[<span class="type">root</span>@<span class="type">server</span> ~]<span class="comment"># ((2&gt;=1));echo $?</span></span><br><span class="line">[<span class="type">root</span>@<span class="type">server</span> ~]<span class="comment"># ((2!=1));echo $?</span></span><br><span class="line">[<span class="type">root</span>@<span class="type">server</span> ~]<span class="comment"># ((`id -u`==0));echo $?</span></span><br><span class="line"> </span><br><span class="line">[<span class="type">root</span>@<span class="type">server</span> ~]<span class="comment"># ((a=123));echo $a</span></span><br><span class="line">[<span class="type">root</span>@<span class="type">server</span> ~]<span class="comment"># unset a</span></span><br><span class="line">[<span class="type">root</span>@<span class="type">server</span> ~]<span class="comment"># ((a==123));echo $?</span></span><br></pre></td></tr></table></figure>
<ul>
<li>字符串比较</li>
</ul>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line">注意：双引号引起来，看作一个整体；= 和 == 在 [ 字符串 ] 比较中都表示判断</span><br><span class="line">[<span class="type">root</span>@<span class="type">server</span> ~]<span class="comment"># a=&#x27;hello world&#x27;;b=world</span></span><br><span class="line">[<span class="type">root</span>@<span class="type">server</span> ~]<span class="comment"># [ $a = $b ];echo $?</span></span><br><span class="line">[<span class="type">root</span>@<span class="type">server</span> ~]<span class="comment"># [ &quot;$a&quot; = &quot;$b&quot; ];echo $?</span></span><br><span class="line">[<span class="type">root</span>@<span class="type">server</span> ~]<span class="comment"># [ &quot;$a&quot; != &quot;$b&quot; ];echo $?</span></span><br><span class="line">[<span class="type">root</span>@<span class="type">server</span> ~]<span class="comment"># [ &quot;$a&quot; !== &quot;$b&quot; ];echo $?        错误</span></span><br><span class="line">[<span class="type">root</span>@<span class="type">server</span> ~]<span class="comment"># [ &quot;$a&quot; == &quot;$b&quot; ];echo $?</span></span><br><span class="line">[<span class="type">root</span>@<span class="type">server</span> ~]<span class="comment"># test &quot;$a&quot; != &quot;$b&quot;;echo $?</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">test  表达式</span><br><span class="line">[ 表达式 ]</span><br><span class="line">[[ 表达式 ]]</span><br><span class="line"></span><br><span class="line">思考：[ ] 和 [[ ]] 有什么区别？</span><br><span class="line"></span><br><span class="line">[<span class="type">root</span>@<span class="type">server</span> ~]<span class="comment"># a=</span></span><br><span class="line">[<span class="type">root</span>@<span class="type">server</span> ~]<span class="comment"># test -z $a;echo $?</span></span><br><span class="line">[<span class="type">root</span>@<span class="type">server</span> ~]<span class="comment"># a=hello</span></span><br><span class="line">[<span class="type">root</span>@<span class="type">server</span> ~]<span class="comment"># test -z $a;echo $?</span></span><br><span class="line">[<span class="type">root</span>@<span class="type">server</span> ~]<span class="comment"># test -n $a;echo $?</span></span><br><span class="line">[<span class="type">root</span>@<span class="type">server</span> ~]<span class="comment"># test -n &quot;$a&quot;;echo $?</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># [ &#x27;&#x27; = $a ];echo $?</span></span><br><span class="line"><span class="literal">-bash</span>: [: : <span class="type">unary</span> <span class="type">operator</span> <span class="type">expected</span></span><br><span class="line"><span class="number">2</span></span><br><span class="line"><span class="comment"># [[ &#x27;&#x27; = $a ]];echo $?</span></span><br><span class="line"><span class="number">0</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">[<span class="type">root</span>@<span class="type">server</span> ~]<span class="comment"># [ 1 -eq 0 -a 1 -ne 0 ];echo $?</span></span><br><span class="line">[<span class="type">root</span>@<span class="type">server</span> ~]<span class="comment"># [ 1 -eq 0 &amp;&amp; 1 -ne 0 ];echo $?</span></span><br><span class="line">[<span class="type">root</span>@<span class="type">server</span> ~]<span class="comment"># [[ 1 -eq 0 &amp;&amp; 1 -ne 0 ]];echo $?</span></span><br></pre></td></tr></table></figure>
<h4 id="②-逻辑运算符总结"><a href="#②-逻辑运算符总结" class="headerlink" title="② 逻辑运算符总结"></a>② 逻辑运算符总结</h4><ol>
<li>符号;和&amp;&amp;和||都可以用来分割命令或者表达式</li>
<li>分号（;）完全不考虑前面的语句是否正确执行，都会执行;号后面的内容</li>
<li><code>&amp;&amp;</code>符号，需要考虑&amp;&amp;前面的语句的正确性，前面语句正确执行才会执行&amp;&amp;后的内容；反之亦然</li>
<li><code>||</code>符号，需要考虑||前面的语句的非正确性，前面语句执行错误才会执行||后内容；反之亦然</li>
<li>如果&amp;&amp;和||一起出现，从左往右依次看，按照以上原则</li>
</ol>
<h1 id="二、流程控制语句"><a href="#二、流程控制语句" class="headerlink" title="二、流程控制语句"></a>二、流程控制语句</h1><p><strong>关键词：==选择==（人生漫漫长路，我该何去何从:vertical_traffic_light:）</strong></p>
<h2 id="1-基本语法结构"><a href="#1-基本语法结构" class="headerlink" title="1. 基本语法结构"></a>1. 基本语法结构</h2><h3 id="㈠-if结构"><a href="#㈠-if结构" class="headerlink" title="㈠ ==if结构=="></a>㈠ ==if结构==</h3><p><strong>箴言1：只要正确，就要一直向前冲:v:</strong></p>
<p><strong>==F==</strong>:表示false，为假</p>
<p><strong>==T==</strong>:表示true，为真</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> [ <span class="type">condition</span> ];then</span><br><span class="line">		command</span><br><span class="line">		command</span><br><span class="line">fi</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> test 条件;then</span><br><span class="line">	命令</span><br><span class="line">fi</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> [[ 条件 ]];then</span><br><span class="line">	命令</span><br><span class="line">fi</span><br><span class="line"></span><br><span class="line">[ 条件 ] &amp;&amp; command</span><br></pre></td></tr></table></figure>
<p><img src="/.com//%E6%B5%81%E7%A8%8B%E5%88%A4%E6%96%AD1.png" alt="流程判断1"></p>
<h3 id="㈡-if…else结构"><a href="#㈡-if…else结构" class="headerlink" title="㈡ ==if…else结构=="></a>㈡ ==if…else结构==</h3><p><strong>箴言2：分叉路口，二选一</strong></p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> [ <span class="type">condition</span> ];then</span><br><span class="line">		command1</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">		command2</span><br><span class="line">fi</span><br><span class="line"></span><br><span class="line">[ 条件 ] &amp;&amp; command1 || command2</span><br></pre></td></tr></table></figure>
<p><img src="/.com//%E6%B5%81%E7%A8%8B%E5%88%A4%E6%96%AD2.png" alt="流程判断2"></p>
<p><strong>小试牛刀：</strong></p>
<p>==让用户自己输入==字符串，==如果==用户输入的是hello，请打印world，==否则==打印“请输入hello”</p>
<ol>
<li><code>read定义变量</code></li>
<li>if….else…</li>
</ol>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#!/bin/env bash</span></span><br><span class="line"></span><br><span class="line">read <span class="literal">-p</span> <span class="string">&#x27;请输入一个字符串:&#x27;</span> str</span><br><span class="line"><span class="keyword">if</span> [ <span class="string">&quot;<span class="variable">$str</span>&quot;</span> = <span class="string">&#x27;hello&#x27;</span> ];then</span><br><span class="line">    <span class="built_in">echo</span> <span class="string">&#x27;world&#x27;</span></span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line">    <span class="built_in">echo</span> <span class="string">&#x27;请输入hello!&#x27;</span></span><br><span class="line">fi</span><br><span class="line"></span><br><span class="line">  <span class="number">1</span> <span class="comment">#!/bin/env bash</span></span><br><span class="line">  <span class="number">2</span></span><br><span class="line">  <span class="number">3</span> read <span class="literal">-p</span> <span class="string">&quot;请输入一个字符串:&quot;</span> str</span><br><span class="line">  <span class="number">4</span> <span class="keyword">if</span> [ <span class="string">&quot;<span class="variable">$str</span>&quot;</span> = <span class="string">&quot;hello&quot;</span> ]</span><br><span class="line">  <span class="number">5</span> then</span><br><span class="line">  <span class="number">6</span>     <span class="built_in">echo</span> world</span><br><span class="line">  <span class="number">7</span> <span class="keyword">else</span></span><br><span class="line">  <span class="number">8</span>     <span class="built_in">echo</span> <span class="string">&quot;请输入hello!&quot;</span></span><br><span class="line">  <span class="number">9</span> fi</span><br><span class="line">  </span><br><span class="line">  <span class="built_in">echo</span> <span class="string">&quot;该脚本需要传递参数&quot;</span></span><br><span class="line">  <span class="number">1</span> <span class="keyword">if</span> [ <span class="variable">$1</span> = <span class="type">hello</span> ];then</span><br><span class="line">  <span class="number">2</span>         <span class="built_in">echo</span> <span class="string">&quot;hello&quot;</span></span><br><span class="line">  <span class="number">3</span> <span class="keyword">else</span></span><br><span class="line">  <span class="number">4</span>         <span class="built_in">echo</span> <span class="string">&quot;请输入hello&quot;</span></span><br><span class="line">  <span class="number">5</span> fi</span><br><span class="line"></span><br><span class="line"><span class="comment">#!/bin/env bash</span></span><br><span class="line"></span><br><span class="line">A=hello</span><br><span class="line">B=world</span><br><span class="line">C=hello</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> [ <span class="string">&quot;<span class="variable">$1</span>&quot;</span> = <span class="string">&quot;<span class="variable">$A</span>&quot;</span> ];then</span><br><span class="line">        <span class="built_in">echo</span> <span class="string">&quot;<span class="variable">$B</span>&quot;</span></span><br><span class="line">    <span class="keyword">else</span></span><br><span class="line">        <span class="built_in">echo</span> <span class="string">&quot;<span class="variable">$C</span>&quot;</span></span><br><span class="line">fi</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">read <span class="literal">-p</span> <span class="string">&#x27;请输入一个字符串:&#x27;</span> str;</span><br><span class="line"> [ <span class="string">&quot;<span class="variable">$str</span>&quot;</span> = <span class="string">&#x27;hello&#x27;</span> ] &amp;&amp; <span class="built_in">echo</span> <span class="string">&#x27;world&#x27;</span> ||  <span class="built_in">echo</span> <span class="string">&#x27;请输入hello!&#x27;</span></span><br></pre></td></tr></table></figure>


<h3 id="㈢-if…elif…else结构"><a href="#㈢-if…elif…else结构" class="headerlink" title="㈢ ==if…elif…else结构=="></a>㈢ ==if…elif…else结构==</h3><p><strong>箴言3：选择很多，能走的只有一条</strong></p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> [ <span class="type">condition1</span> ];then</span><br><span class="line">		command1  	结束</span><br><span class="line">	elif [ <span class="type">condition2</span> ];then</span><br><span class="line">		command2   	结束</span><br><span class="line">	<span class="keyword">else</span></span><br><span class="line">		command3</span><br><span class="line">fi</span><br><span class="line">注释：</span><br><span class="line">如果条件<span class="number">1</span>满足，执行命令<span class="number">1</span>后结束；如果条件<span class="number">1</span>不满足，再看条件<span class="number">2</span>，如果条件<span class="number">2</span>满足执行命令<span class="number">2</span>后结束；如果条件<span class="number">1</span>和条件<span class="number">2</span>都不满足执行命令<span class="number">3</span>结束.</span><br></pre></td></tr></table></figure>
<p><img src="/.com//%E6%B5%81%E7%A8%8B%E5%88%A4%E6%96%AD3.png" alt="流程判断3"></p>
<h3 id="㈣-层层嵌套结构"><a href="#㈣-层层嵌套结构" class="headerlink" title="㈣ 层层嵌套结构"></a>㈣ 层层嵌套结构</h3><p><strong>箴言4：多次判断，带你走出人生迷雾。</strong></p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> [ <span class="type">condition1</span> ];then</span><br><span class="line">		command1		</span><br><span class="line">		<span class="keyword">if</span> [ <span class="type">condition2</span> ];then</span><br><span class="line">			command2</span><br><span class="line">		fi</span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line">		<span class="keyword">if</span> [ <span class="type">condition3</span> ];then</span><br><span class="line">			command3</span><br><span class="line">		elif [ <span class="type">condition4</span> ];then</span><br><span class="line">			command4</span><br><span class="line">		<span class="keyword">else</span></span><br><span class="line">			command5</span><br><span class="line">		fi</span><br><span class="line">fi</span><br><span class="line">注释：</span><br><span class="line">如果条件<span class="number">1</span>满足，执行命令<span class="number">1</span>；如果条件<span class="number">2</span>也满足执行命令<span class="number">2</span>，如果不满足就只执行命令<span class="number">1</span>结束；</span><br><span class="line">如果条件<span class="number">1</span>不满足，不看条件<span class="number">2</span>；直接看条件<span class="number">3</span>，如果条件<span class="number">3</span>满足执行命令<span class="number">3</span>；如果不满足则看条件<span class="number">4</span>，如果条件<span class="number">4</span>满足执行命令<span class="number">4</span>；否则执行命令<span class="number">5</span></span><br></pre></td></tr></table></figure>
<p><img src="/.com//%E6%B5%81%E7%A8%8B%E5%88%A4%E6%96%AD4.png" alt="流程判断4"></p>
<h2 id="2-应用案例"><a href="#2-应用案例" class="headerlink" title="2. 应用案例"></a>2. 应用案例</h2><h3 id="㈠-判断两台主机是否ping通"><a href="#㈠-判断两台主机是否ping通" class="headerlink" title="㈠ 判断两台主机是否ping通"></a>㈠ 判断两台主机是否ping通</h3><p><strong>需求：</strong>判断==当前主机==是否和==远程主机==是否ping通</p>
<h4 id="①-思路"><a href="#①-思路" class="headerlink" title="① 思路"></a>① 思路</h4><ol>
<li>使用哪个命令实现  <code>ping -c次数</code></li>
<li>根据命令的==执行结果状态==来判断是否通<code>$?</code></li>
<li>根据逻辑和语法结构来编写脚本(条件判断或者流程控制)</li>
</ol>
<h4 id="②-落地实现"><a href="#②-落地实现" class="headerlink" title="② 落地实现"></a>② 落地实现</h4><figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#!/bin/env bash</span></span><br><span class="line"><span class="comment"># 该脚本用于判断当前主机是否和远程指定主机互通</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 交互式定义变量，让用户自己决定ping哪个主机</span></span><br><span class="line">read <span class="literal">-p</span> <span class="string">&quot;请输入你要ping的主机的IP:&quot;</span> ip</span><br><span class="line"></span><br><span class="line"><span class="comment"># 使用ping程序判断主机是否互通</span></span><br><span class="line">ping <span class="literal">-c1</span> <span class="variable">$ip</span> &amp;&gt;/dev/null</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> [ <span class="variable">$</span>? -<span class="type">eq</span> <span class="number">0</span> ];then</span><br><span class="line">	<span class="built_in">echo</span> <span class="string">&quot;当前主机和远程主机<span class="variable">$ip</span>是互通的&quot;</span></span><br><span class="line"> <span class="keyword">else</span></span><br><span class="line"> 	<span class="built_in">echo</span> <span class="string">&quot;当前主机和远程主机<span class="variable">$ip</span>不通的&quot;</span></span><br><span class="line">fi</span><br><span class="line"></span><br><span class="line">逻辑运算符</span><br><span class="line">test <span class="variable">$</span>? <span class="operator">-eq</span> <span class="number">0</span> &amp;&amp;  <span class="built_in">echo</span> <span class="string">&quot;当前主机和远程主机<span class="variable">$ip</span>是互通的&quot;</span> || <span class="built_in">echo</span> <span class="string">&quot;当前主机和远程主机<span class="variable">$ip</span>不通的&quot;</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h3 id="㈡-判断一个进程是否存在"><a href="#㈡-判断一个进程是否存在" class="headerlink" title="㈡ 判断一个进程是否存在"></a>㈡ 判断一个进程是否存在</h3><p><strong>需求：</strong>判断web服务器中httpd进程是否存在</p>
<h4 id="①-思路-1"><a href="#①-思路-1" class="headerlink" title="① 思路"></a>① 思路</h4><ol>
<li>查看进程的相关命令   ps   pgrep</li>
<li>根据命令的返回状态值来判断进程是否存在</li>
<li>根据逻辑用脚本语言实现</li>
</ol>
<h4 id="②-落地实现-1"><a href="#②-落地实现-1" class="headerlink" title="② 落地实现"></a>② 落地实现</h4><figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#!/bin/env bash</span></span><br><span class="line"><span class="comment"># 判断一个程序(httpd)的进程是否存在</span></span><br><span class="line">pgrep httpd &amp;&gt;/dev/null</span><br><span class="line"><span class="keyword">if</span> [ <span class="variable">$</span>? -<span class="type">ne</span> <span class="number">0</span> ];then</span><br><span class="line">	<span class="built_in">echo</span> <span class="string">&quot;当前httpd进程不存在&quot;</span></span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">	<span class="built_in">echo</span> <span class="string">&quot;当前httpd进程存在&quot;</span></span><br><span class="line">fi</span><br><span class="line"></span><br><span class="line">或者</span><br><span class="line">test <span class="variable">$</span>? <span class="operator">-eq</span> <span class="number">0</span> &amp;&amp; <span class="built_in">echo</span> <span class="string">&quot;当前httpd进程存在&quot;</span> || <span class="built_in">echo</span> <span class="string">&quot;当前httpd进程不存在&quot;</span></span><br></pre></td></tr></table></figure>
<h4 id="③-补充命令"><a href="#③-补充命令" class="headerlink" title="③ 补充命令"></a>③ 补充命令</h4><figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">pgrep命令：以名称为依据从运行进程队列中查找进程，并显示查找到的进程id</span><br><span class="line">选项</span><br><span class="line"><span class="literal">-o</span>：仅显示找到的最小（起始）进程号;</span><br><span class="line"><span class="literal">-n</span>：仅显示找到的最大（结束）进程号；</span><br><span class="line"><span class="literal">-l</span>：显示进程名称；</span><br><span class="line"><span class="literal">-P</span>：指定父进程号；pgrep <span class="literal">-p</span> <span class="number">4764</span>  查看父进程下的子进程id</span><br><span class="line"><span class="literal">-g</span>：指定进程组；</span><br><span class="line"><span class="literal">-t</span>：指定开启进程的终端；</span><br><span class="line"><span class="literal">-u</span>：指定进程的有效用户ID。</span><br></pre></td></tr></table></figure>
<h3 id="㈢-判断一个服务是否正常"><a href="#㈢-判断一个服务是否正常" class="headerlink" title="㈢ 判断一个服务是否正常"></a>㈢ 判断一个服务是否正常</h3><p><strong>需求：</strong>判断门户网站是否能够正常访问</p>
<h4 id="①-思路-2"><a href="#①-思路-2" class="headerlink" title="① 思路"></a>① 思路</h4><ol>
<li>可以判断进程是否存在，用/etc/init.d/httpd status判断状态等方法</li>
<li>最好的方法是==直接去访问==一下，通过访问成功和失败的返回值来判断<ul>
<li>Linux环境，==wget==  curl  elinks -dump</li>
</ul>
</li>
</ol>
<h4 id="②-落地实现-2"><a href="#②-落地实现-2" class="headerlink" title="② 落地实现"></a>② 落地实现</h4><figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#!/bin/env bash</span></span><br><span class="line"><span class="comment"># 判断门户网站是否能够正常提供服务</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#定义变量</span></span><br><span class="line">web_server=www.itcast.cn</span><br><span class="line"><span class="comment">#访问网站</span></span><br><span class="line"><span class="built_in">wget</span> <span class="literal">-P</span> /shell/ <span class="variable">$web_server</span> &amp;&gt;/dev/null</span><br><span class="line">[ <span class="variable">$</span>? -<span class="type">eq</span> <span class="number">0</span> ] &amp;&amp; <span class="built_in">echo</span> <span class="string">&quot;当前网站服务是ok&quot;</span> &amp;&amp; <span class="built_in">rm</span> <span class="operator">-f</span> /shell/index.* || <span class="built_in">echo</span> <span class="string">&quot;当前网站服务不ok，请立刻处理&quot;</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>##3. 课堂练习</p>
<h3 id="㈠-判断用户是否存在"><a href="#㈠-判断用户是否存在" class="headerlink" title="㈠ 判断用户是否存在"></a>㈠ 判断用户是否存在</h3><p><strong>需求1：</strong>输入一个用户，用脚本判断该用户是否存在</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><span class="line"> <span class="comment">#!/bin/env bash</span></span><br><span class="line">  <span class="number">2</span> read <span class="literal">-p</span> <span class="string">&quot;请输入一个用户名：&quot;</span> user_name</span><br><span class="line">  <span class="number">3</span> id <span class="variable">$user_name</span> &amp;&gt;/dev/null</span><br><span class="line">  <span class="number">4</span> <span class="keyword">if</span> [ <span class="variable">$</span>? -<span class="type">eq</span> <span class="number">0</span> ];then</span><br><span class="line">  <span class="number">6</span>     <span class="built_in">echo</span> <span class="string">&quot;该用户存在！&quot;</span></span><br><span class="line">  <span class="number">7</span> <span class="keyword">else</span></span><br><span class="line">  <span class="number">8</span>     <span class="built_in">echo</span> <span class="string">&quot;用户不存在！&quot;</span></span><br><span class="line">  <span class="number">9</span> fi</span><br><span class="line">  </span><br><span class="line">  </span><br><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line"><span class="comment"># 判断 用户（id） 是否存在</span></span><br><span class="line">read <span class="literal">-p</span> <span class="string">&quot;输入壹个用户：&quot;</span> id</span><br><span class="line">id <span class="variable">$id</span> &amp;&gt; /dev/null</span><br><span class="line"><span class="keyword">if</span> [ <span class="variable">$</span>? -<span class="type">eq</span> <span class="number">0</span> ];then</span><br><span class="line">        <span class="built_in">echo</span> <span class="string">&quot;该用户存在&quot;</span></span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">        <span class="built_in">echo</span> <span class="string">&quot;该用户不存在&quot;</span></span><br><span class="line">fi</span><br><span class="line"></span><br><span class="line"><span class="comment">#!/bin/env bash</span></span><br><span class="line">read <span class="literal">-p</span> <span class="string">&quot;请输入你要查询的用户名:&quot;</span> username</span><br><span class="line">grep <span class="literal">-w</span> <span class="variable">$username</span> /etc/passwd &amp;&gt;/dev/null</span><br><span class="line"><span class="keyword">if</span> [ <span class="variable">$</span>? -<span class="type">eq</span> <span class="number">0</span> ]</span><br><span class="line">then</span><br><span class="line">    <span class="built_in">echo</span> <span class="string">&quot;该用户已存在&quot;</span></span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">    <span class="built_in">echo</span> <span class="string">&quot;该用户不存在&quot;</span></span><br><span class="line">fi</span><br><span class="line"></span><br><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line">read <span class="literal">-p</span> <span class="string">&quot;请输入你要检查的用户名：&quot;</span> name</span><br><span class="line">id <span class="variable">$name</span> &amp;&gt;/dev/null</span><br><span class="line"><span class="keyword">if</span> [ <span class="variable">$</span>? -<span class="type">eq</span> <span class="number">0</span> ]</span><br><span class="line">then</span><br><span class="line"><span class="built_in">echo</span> 用户<span class="string">&quot;<span class="variable">$name</span>&quot;</span>已经存在</span><br><span class="line"><span class="keyword">else</span></span><br><span class="line"><span class="built_in">echo</span> 用户<span class="string">&quot;<span class="variable">$name</span>&quot;</span>不存在</span><br><span class="line">fi</span><br><span class="line"></span><br><span class="line"><span class="comment">#!/bin/env bash</span></span><br><span class="line"><span class="comment">#判断用户是否存在</span></span><br><span class="line">read <span class="literal">-p</span> <span class="string">&quot;请写出用户名&quot;</span> id</span><br><span class="line">id <span class="variable">$id</span></span><br><span class="line"><span class="keyword">if</span> [ <span class="variable">$</span>? -<span class="type">eq</span> <span class="number">0</span> ];then</span><br><span class="line">        <span class="built_in">echo</span> <span class="string">&quot;用户存在&quot;</span></span><br><span class="line"><span class="keyword">else</span></span><br><span class="line">        <span class="built_in">echo</span> <span class="string">&quot;用户不存在&quot;</span></span><br><span class="line">fi</span><br><span class="line"></span><br><span class="line"><span class="comment">#!/bin/env bash</span></span><br><span class="line">read <span class="literal">-p</span> <span class="string">&#x27;请输入用户名:&#x27;</span> username</span><br><span class="line">id <span class="variable">$username</span> &amp;&gt;/dev/null</span><br><span class="line">[ <span class="variable">$</span>? -<span class="type">eq</span> <span class="number">0</span> ] &amp;&amp; <span class="built_in">echo</span> <span class="string">&#x27;用户存在&#x27;</span> || <span class="built_in">echo</span> <span class="string">&#x27;不存在&#x27;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h3 id="㈡-判断软件包是否安装"><a href="#㈡-判断软件包是否安装" class="headerlink" title="㈡ 判断软件包是否安装"></a>㈡ 判断软件包是否安装</h3><p><strong>需求2：</strong>用脚本判断一个软件包是否安装，如果没安装则安装它（假设本地yum已配合）</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"></span><br></pre></td></tr></table></figure>
<h3 id="㈢-判断当前主机的内核版本"><a href="#㈢-判断当前主机的内核版本" class="headerlink" title="㈢ 判断当前主机的内核版本"></a>㈢ 判断当前主机的内核版本</h3><p><strong>需求3：</strong>判断当前内核主版本是否为2，且次版本是否大于等于6；如果都满足则输出当前内核版本</p>
<figure class="highlight powershell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">思路：</span><br><span class="line"><span class="number">1</span>. 先查看内核的版本号	uname <span class="literal">-r</span></span><br><span class="line"><span class="number">2</span>. 先将内核的版本号保存到一个变量里，然后再根据需求截取出该变量的一部分：主版本和次版本</span><br><span class="line"><span class="number">3</span>. 根据需求进步判断</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line">kernel=`uname <span class="literal">-r</span>`</span><br><span class="line">var1=`echo <span class="variable">$kernel</span>|cut <span class="literal">-d</span>. <span class="literal">-f1</span>`</span><br><span class="line">var2=`echo <span class="variable">$kernel</span>|cut <span class="literal">-d</span>. <span class="literal">-f2</span>`</span><br><span class="line">test <span class="variable">$var1</span> <span class="operator">-eq</span> <span class="number">2</span> <span class="literal">-a</span> <span class="variable">$var2</span> <span class="operator">-ge</span> <span class="number">6</span> &amp;&amp; <span class="built_in">echo</span> <span class="variable">$kernel</span> || <span class="built_in">echo</span> <span class="string">&quot;当前内核版本不符合要求&quot;</span></span><br><span class="line">或者</span><br><span class="line">[ <span class="variable">$var1</span> -<span class="type">eq</span> <span class="number">2</span> -<span class="type">a</span> <span class="variable">$var2</span> -<span class="type">ge</span> <span class="number">6</span> ] &amp;&amp; <span class="built_in">echo</span> <span class="variable">$kernel</span> || <span class="built_in">echo</span> <span class="string">&quot;当前内核版本不符合要求&quot;</span></span><br><span class="line">或者</span><br><span class="line">[[ <span class="variable">$var1</span> -<span class="type">eq</span> <span class="number">2</span> &amp;&amp; <span class="variable">$var2</span> -<span class="type">ge</span> <span class="number">6</span> ]] &amp;&amp; <span class="built_in">echo</span> <span class="variable">$kernel</span> || <span class="built_in">echo</span> <span class="string">&quot;当前内核版本不符合要求&quot;</span></span><br><span class="line"></span><br><span class="line">或者</span><br><span class="line"><span class="comment">#!/bin/bash</span></span><br><span class="line">kernel=`uname <span class="literal">-r</span>`</span><br><span class="line">test <span class="variable">$</span>&#123;kernel:<span class="number">0</span>:<span class="number">1</span>&#125; <span class="operator">-eq</span> <span class="number">2</span> <span class="literal">-a</span> <span class="variable">$</span>&#123;kernel:<span class="number">2</span>:<span class="number">1</span>&#125; <span class="operator">-ge</span> <span class="number">6</span> &amp;&amp; <span class="built_in">echo</span> <span class="variable">$kernel</span> || <span class="built_in">echo</span> <span class="string">&#x27;不符合要求&#x27;</span></span><br><span class="line"></span><br><span class="line">其他命令参考：</span><br><span class="line">uname <span class="literal">-r</span>|grep ^<span class="number">2</span>.[<span class="number">6</span>-<span class="number">9</span>] || <span class="built_in">echo</span> <span class="string">&#x27;不符合要求&#x27;</span></span><br></pre></td></tr></table></figure>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/linux/" rel="tag"># linux</a>
              <a href="/tags/shell/" rel="tag"># shell</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2021/01/03/%E5%B0%8F%E7%94%9F%E5%8D%9A%E5%AE%A2/java/thymeleaf%E6%96%87%E6%A1%A3/" rel="prev" title="thymeleaf 使用">
      <i class="fa fa-chevron-left"></i> thymeleaf 使用
    </a></div>
      <div class="post-nav-item">
    <a href="/2021/01/04/%E5%B0%8F%E7%94%9F%E5%8D%9A%E5%AE%A2/%E6%95%B0%E6%8D%AE%E5%BA%93/ElasticSearch%E6%96%87%E6%A1%A3/" rel="next" title="ElasticSearch 使用">
      ElasticSearch 使用 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E6%9C%AC%E8%8A%82%E8%AF%BE%E7%A8%8B%E7%9B%AE%E6%A0%87"><span class="nav-number">1.</span> <span class="nav-text">本节课程目标</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%80%E3%80%81%E6%9D%A1%E4%BB%B6%E5%88%A4%E6%96%AD%E8%AF%AD%E6%B3%95%E7%BB%93%E6%9E%84"><span class="nav-number">2.</span> <span class="nav-text">一、条件判断语法结构</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E6%9D%A1%E4%BB%B6%E5%88%A4%E6%96%AD%E7%9B%B8%E5%85%B3%E5%8F%82%E6%95%B0"><span class="nav-number">2.1.</span> <span class="nav-text">2. 条件判断相关参数</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E3%88%A0-%E5%88%A4%E6%96%AD%E6%96%87%E4%BB%B6%E7%B1%BB%E5%9E%8B"><span class="nav-number">2.1.1.</span> <span class="nav-text">㈠ &#x3D;&#x3D;判断文件类型&#x3D;&#x3D;</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E3%88%A1-%E5%88%A4%E6%96%AD%E6%96%87%E4%BB%B6%E6%9D%83%E9%99%90"><span class="nav-number">2.1.2.</span> <span class="nav-text">㈡ 判断文件权限</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E3%88%A2-%E5%88%A4%E6%96%AD%E6%96%87%E4%BB%B6%E6%96%B0%E6%97%A7"><span class="nav-number">2.1.3.</span> <span class="nav-text">㈢ 判断文件新旧</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E3%88%A3-%E5%88%A4%E6%96%AD%E6%95%B4%E6%95%B0"><span class="nav-number">2.1.4.</span> <span class="nav-text">㈣ 判断整数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E3%88%A4-%E5%88%A4%E6%96%AD%E5%AD%97%E7%AC%A6%E4%B8%B2"><span class="nav-number">2.1.5.</span> <span class="nav-text">㈤ &#x3D;&#x3D;判断字符串&#x3D;&#x3D;</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E3%88%A5-%E5%A4%9A%E9%87%8D%E6%9D%A1%E4%BB%B6%E5%88%A4%E6%96%AD"><span class="nav-number">2.1.6.</span> <span class="nav-text">㈥ &#x3D;&#x3D;多重条件判断&#x3D;&#x3D;</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E2%91%A0-%E4%B8%BE%E4%BE%8B%E8%AF%B4%E6%98%8E"><span class="nav-number">2.1.6.1.</span> <span class="nav-text">① 举例说明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E2%91%A1-%E9%80%BB%E8%BE%91%E8%BF%90%E7%AE%97%E7%AC%A6%E6%80%BB%E7%BB%93"><span class="nav-number">2.1.6.2.</span> <span class="nav-text">② 逻辑运算符总结</span></a></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%8C%E3%80%81%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6%E8%AF%AD%E5%8F%A5"><span class="nav-number">3.</span> <span class="nav-text">二、流程控制语句</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E5%9F%BA%E6%9C%AC%E8%AF%AD%E6%B3%95%E7%BB%93%E6%9E%84"><span class="nav-number">3.1.</span> <span class="nav-text">1. 基本语法结构</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E3%88%A0-if%E7%BB%93%E6%9E%84"><span class="nav-number">3.1.1.</span> <span class="nav-text">㈠ &#x3D;&#x3D;if结构&#x3D;&#x3D;</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E3%88%A1-if%E2%80%A6else%E7%BB%93%E6%9E%84"><span class="nav-number">3.1.2.</span> <span class="nav-text">㈡ &#x3D;&#x3D;if…else结构&#x3D;&#x3D;</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E3%88%A2-if%E2%80%A6elif%E2%80%A6else%E7%BB%93%E6%9E%84"><span class="nav-number">3.1.3.</span> <span class="nav-text">㈢ &#x3D;&#x3D;if…elif…else结构&#x3D;&#x3D;</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E3%88%A3-%E5%B1%82%E5%B1%82%E5%B5%8C%E5%A5%97%E7%BB%93%E6%9E%84"><span class="nav-number">3.1.4.</span> <span class="nav-text">㈣ 层层嵌套结构</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E5%BA%94%E7%94%A8%E6%A1%88%E4%BE%8B"><span class="nav-number">3.2.</span> <span class="nav-text">2. 应用案例</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E3%88%A0-%E5%88%A4%E6%96%AD%E4%B8%A4%E5%8F%B0%E4%B8%BB%E6%9C%BA%E6%98%AF%E5%90%A6ping%E9%80%9A"><span class="nav-number">3.2.1.</span> <span class="nav-text">㈠ 判断两台主机是否ping通</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E2%91%A0-%E6%80%9D%E8%B7%AF"><span class="nav-number">3.2.1.1.</span> <span class="nav-text">① 思路</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E2%91%A1-%E8%90%BD%E5%9C%B0%E5%AE%9E%E7%8E%B0"><span class="nav-number">3.2.1.2.</span> <span class="nav-text">② 落地实现</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E3%88%A1-%E5%88%A4%E6%96%AD%E4%B8%80%E4%B8%AA%E8%BF%9B%E7%A8%8B%E6%98%AF%E5%90%A6%E5%AD%98%E5%9C%A8"><span class="nav-number">3.2.2.</span> <span class="nav-text">㈡ 判断一个进程是否存在</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E2%91%A0-%E6%80%9D%E8%B7%AF-1"><span class="nav-number">3.2.2.1.</span> <span class="nav-text">① 思路</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E2%91%A1-%E8%90%BD%E5%9C%B0%E5%AE%9E%E7%8E%B0-1"><span class="nav-number">3.2.2.2.</span> <span class="nav-text">② 落地实现</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E2%91%A2-%E8%A1%A5%E5%85%85%E5%91%BD%E4%BB%A4"><span class="nav-number">3.2.2.3.</span> <span class="nav-text">③ 补充命令</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E3%88%A2-%E5%88%A4%E6%96%AD%E4%B8%80%E4%B8%AA%E6%9C%8D%E5%8A%A1%E6%98%AF%E5%90%A6%E6%AD%A3%E5%B8%B8"><span class="nav-number">3.2.3.</span> <span class="nav-text">㈢ 判断一个服务是否正常</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E2%91%A0-%E6%80%9D%E8%B7%AF-2"><span class="nav-number">3.2.3.1.</span> <span class="nav-text">① 思路</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E2%91%A1-%E8%90%BD%E5%9C%B0%E5%AE%9E%E7%8E%B0-2"><span class="nav-number">3.2.3.2.</span> <span class="nav-text">② 落地实现</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E3%88%A0-%E5%88%A4%E6%96%AD%E7%94%A8%E6%88%B7%E6%98%AF%E5%90%A6%E5%AD%98%E5%9C%A8"><span class="nav-number">3.2.4.</span> <span class="nav-text">㈠ 判断用户是否存在</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E3%88%A1-%E5%88%A4%E6%96%AD%E8%BD%AF%E4%BB%B6%E5%8C%85%E6%98%AF%E5%90%A6%E5%AE%89%E8%A3%85"><span class="nav-number">3.2.5.</span> <span class="nav-text">㈡ 判断软件包是否安装</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E3%88%A2-%E5%88%A4%E6%96%AD%E5%BD%93%E5%89%8D%E4%B8%BB%E6%9C%BA%E7%9A%84%E5%86%85%E6%A0%B8%E7%89%88%E6%9C%AC"><span class="nav-number">3.2.6.</span> <span class="nav-text">㈢ 判断当前主机的内核版本</span></a></li></ol></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="王增明"
      src="/images/avatar.jpeg">
  <p class="site-author-name" itemprop="name">王增明</p>
  <div class="site-description" itemprop="description">喜欢折腾的极客聚集地.</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">23</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">6</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">18</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL3dhbnplbmdtaW5n" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;wanzengming"><i class="fab fa-github fa-fw"></i>GitHub</span>
      </span>
      <span class="links-of-author-item">
        <a href="/wangzengming@aliyun.com" title="E-Mail → wangzengming@aliyun.com"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">王增明</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
      <span class="post-meta-item-text">站点总字数：</span>
    <span title="站点总字数">342k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span class="post-meta-item-text">站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">5:11</span>
</div>

        
<div class="busuanzi-count">
  <script data-pjax async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/pjax/pjax.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>

<script src="/js/bookmark.js"></script>

  <script>
var pjax = new Pjax({
  selectors: [
    'head title',
    '#page-configurations',
    '.content-wrap',
    '.post-toc-wrap',
    '.languages',
    '#pjax'
  ],
  switches: {
    '.post-toc-wrap': Pjax.switches.innerHTML
  },
  analytics: false,
  cacheBust: false,
  scrollTo : !CONFIG.bookmark.enable
});

window.addEventListener('pjax:success', () => {
  document.querySelectorAll('script[data-pjax], script#page-configurations, #pjax script').forEach(element => {
    var code = element.text || element.textContent || element.innerHTML || '';
    var parent = element.parentNode;
    parent.removeChild(element);
    var script = document.createElement('script');
    if (element.id) {
      script.id = element.id;
    }
    if (element.className) {
      script.className = element.className;
    }
    if (element.type) {
      script.type = element.type;
    }
    if (element.src) {
      script.src = element.src;
      // Force synchronous loading of peripheral JS.
      script.async = false;
    }
    if (element.dataset.pjax !== undefined) {
      script.dataset.pjax = '';
    }
    if (code !== '') {
      script.appendChild(document.createTextNode(code));
    }
    parent.appendChild(script);
  });
  NexT.boot.refresh();
  // Define Motion Sequence & Bootstrap Motion.
  if (CONFIG.motion.enable) {
    NexT.motion.integrator
      .init()
      .add(NexT.motion.middleWares.subMenu)
      .add(NexT.motion.middleWares.postList)
      .bootstrap();
  }
  NexT.utils.updateSidebarPosition();
});
</script>


  <script defer src="/lib/three/three.min.js"></script>
    <script defer src="/lib/three/three-waves.min.js"></script>
    <script defer src="/lib/three/canvas_lines.min.js"></script>
    <script defer src="/lib/three/canvas_sphere.min.js"></script>


  
  <script data-pjax>
    (function(){
      var canonicalURL, curProtocol;
      //Get the <link> tag
      var x=document.getElementsByTagName("link");
		//Find the last canonical URL
		if(x.length > 0){
			for (i=0;i<x.length;i++){
				if(x[i].rel.toLowerCase() == 'canonical' && x[i].href){
					canonicalURL=x[i].href;
				}
			}
		}
    //Get protocol
	    if (!canonicalURL){
	    	curProtocol = window.location.protocol.split(':')[0];
	    }
	    else{
	    	curProtocol = canonicalURL.split(':')[0];
	    }
      //Get current URL if the canonical URL does not exist
	    if (!canonicalURL) canonicalURL = window.location.href;
	    //Assign script content. Replace current URL with the canonical URL
      !function(){var e=/([http|https]:\/\/[a-zA-Z0-9\_\.]+\.baidu\.com)/gi,r=canonicalURL,t=document.referrer;if(!e.test(r)){var n=(String(curProtocol).toLowerCase() === 'https')?"https://sp0.baidu.com/9_Q4simg2RQJ8t7jm9iCKT-xh_/s.gif":"//api.share.baidu.com/s.gif";t?(n+="?r="+encodeURIComponent(document.referrer),r&&(n+="&l="+r)):r&&(n+="?l="+r);var i=new Image;i.src=n}}(window);})();
  </script>




  
<script src="/js/local-search.js"></script>











<script data-pjax>
if (document.querySelectorAll('pre.mermaid').length) {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/mermaid@8/dist/mermaid.min.js', () => {
    mermaid.initialize({
      theme    : 'forest',
      logLevel : 3,
      flowchart: { curve     : 'linear' },
      gantt    : { axisFormat: '%m/%d/%Y' },
      sequence : { actorMargin: 50 }
    });
  }, window.mermaid);
}
</script>


    <div id="pjax">
  

  

    </div>
<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","model":{"jsonPath":"/live2dw/assets/hijiki.model.json"},"display":{"position":"right","width":150,"height":300},"mobile":{"show":false},"react":{"opacityDefault":1,"opacityOnHover":1},"log":false,"tagMode":false});</script></body>
</html>
<script type="text/javascript" src="//cdn.bootcss.com/canvas-nest.js/1.0.0/canvas-nest.min.js"></script>
